﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HM
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            LoadTreeViewDt(tvMain, createDataTableForTV(), "0");
            SetFormWithAndHeith(this);
        }

        /// <summary>
        /// 创建页面的树形结构,后期需完善权限功能
        /// </summary>
        /// <returns></returns>
        public DataTable createDataTableForTV()
        {

            DataTable dt = new DataTable("dtTreeView");

            DataColumn dcol;

            dcol = new DataColumn("RootID", typeof(string));
            dt.Columns.Add(dcol);

            dcol = new DataColumn("RootParentID", typeof(string));
            dt.Columns.Add(dcol);

            dcol = new DataColumn("RootPicIndex", typeof(string));
            dt.Columns.Add(dcol);

            dcol = new DataColumn("RootText", typeof(string));
            dt.Columns.Add(dcol);

            DataRow drow;


            drow = dt.NewRow();
            drow["RootID"] = "1";
            drow["RootParentID"] = "0";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "主菜单";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "101";
            drow["RootParentID"] = "1";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "供应链业务";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "10101";
            drow["RootParentID"] = "101";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "外购入库单业务";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "10102";
            drow["RootParentID"] = "101";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "外购入库单业务(红字)";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "10103";
            drow["RootParentID"] = "101";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "销售出库单业务";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "10104";
            drow["RootParentID"] = "101";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "销售退货管理";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "10105";
            drow["RootParentID"] = "101";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "调拨单业务";
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["RootID"] = "10106";
            drow["RootParentID"] = "101";
            drow["RootPicIndex"] = "0";
            drow["RootText"] = "仓库查询";
            dt.Rows.Add(drow);

            return dt;
        }

        #region 构建菜单树(递归)

        public void LoadTreeViewDt(TreeView tv, DataTable dt, string RootParentID)
        {
            //得到根节点信息
            DataRow[] DataRoot = dt.Select("RootParentID='" + RootParentID + "'");

            //循环遍历根节点信息
            foreach (DataRow dr in DataRoot)
            {
                //加载根节点
                TreeNode tn = new TreeNode();
                tn.Text = dr["RootText"].ToString();
                tn.Tag = dr["RootID"].ToString();
                tv.ImageIndex = Convert.ToInt32(dr["RootPicIndex"]);
                tv.Nodes.Add(tn);

                //递归加载子节点
                LoadNodes(tn, dr, dt);
            }
        }

        private void LoadNodes(TreeNode tn, DataRow dr, DataTable dt)
        {
            if (dr == null || tn == null) { return; }

            //得到子节点信息
            DataRow[] DataChild = dt.Select("RootParentID='" + dr["RootID"] + "'");

            if (DataChild != null || DataChild.Length > 0)
            {
                //循环遍历子节点信息
                foreach (DataRow drChild in DataChild)
                {
                    //加载子节点
                    TreeNode tnChild = new TreeNode();
                    tnChild.Text = drChild["RootText"].ToString();
                    tnChild.Tag = drChild["RootID"].ToString();
                    tnChild.ImageIndex = Convert.ToInt32(dr["RootPicIndex"]);
                    tn.Nodes.Add(tnChild);

                    //形成递归
                    LoadNodes(tnChild, drChild, dt);
                }
            }
        }

        #endregion

        

        private void MainForm_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string modValue;
            modValue = tvMain.SelectedNode.Tag.ToString();
            switch (modValue)
            { 
                case "10101":
                    InStock frm = new InStock();
                    frm.Show();
                    this.Hide();
                    break;
                case "10102":
                    InStockBack frmback = new InStockBack();
                    frmback.Show();
                    this.Hide();
                    break;
                case "10103":
                    OutStock outStock = new OutStock();
                    outStock.Show();
                    this.Hide();
                    break;
                case "10104":
                    OutStockBack outStockBack = new OutStockBack();
                    outStockBack.Show();
                    this.Hide();
                    break;
                case "10106":
                    StockQuery stQuery = new StockQuery();
                    stQuery.Show();
                    this.Hide();
                    break;
                default:
                    MessageBox.Show("请选择相应功能菜单");
                    break;
            }

        }

        private void SetFormWithAndHeith(Form obj)
        {
            Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            int width = ScreenArea.Width;
            int height = ScreenArea.Height;
            obj.Width = width - 5;
            obj.Height = height - 5;
        }
    }
}